import * as cacheTypes from './cache-types';

function cacheReducer(cacheStates,action){
  const payload = action.payload;
  const cacheId = payload.cacheId;

  switch (action.type) {
  case cacheTypes.CREATE:
    return {
      ...cacheStates,
      [cacheId]:{
        cacheId: cacheId, //缓存ID
        reactElement: payload.reactElement, //要渲染的虚拟DOM
        doms:undefined, //此虚拟dom对应的真实dom
        status: cacheTypes.CREATE, //缓存的状态是创建
        scrolls: {} //滚动信息保存对象, 默认 key是滚动的dom 值是滚动的位置
      }
    }
  case cacheTypes.CREATED:
    return {
      ...cacheStates,
      [cacheId]:{
        ...cacheStates[cacheId],
        doms:payload.doms,
        status: cacheTypes.CREATED
      }
    }
  case cacheTypes.DESTROY:
    return {
      ...cacheStates,
      [cacheId]:{
        ...cacheStates[cacheId],
        status: cacheTypes.DESTROY
      }
    }
  default:
    return cacheStates;
  }
}

export default cacheReducer;
